package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.mine.model.UserAsset;
import com.meitianhui.convenitenbuy.module.wallet.adapter.WalletChangeAdapter;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletChangeContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.TradeRecordBean;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletChangePresenter;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletChangeActivity extends BaseToolbarActivity implements WalletChangeContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    private UserAsset bean;
    private TextView mTvChangeNum;
    private TextView mTvFrozen;
    private Button mBtnRecharge;
    private Button mBtnUnfreeze;
    private RecyclerView mRv;
    private LinearLayout mLlNone;
    private WalletChangeAdapter mAdapter;
    private int pageNum = 1;
    @Inject
    WalletChangePresenter mPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        bean = getIntent().getParcelableExtra(JumpUtil.JUMP_WALLET_FRAGMENT_VALUE);
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);

        initId();
        initInfo();
        setTbTitle(getString(R.string.wallet_loose_change));
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_change;
    }

    /**
     * 初始化Id
     */
    private void initId() {
        mTvChangeNum = getViewById(R.id.tv_wallet_change_num);
        mTvFrozen = getViewById(R.id.tv_wallet_change_frozen);
        mRv = getViewById(R.id.rv_wallet_change);
        mBtnRecharge = getViewById(R.id.btn_wallet_change_recharge);
        mBtnUnfreeze = getViewById(R.id.btn_wallet_change_unfreeze);
        mLlNone = getViewById(R.id.ll_wallet_change_none);
        LinearLayoutManager mLinearManager = new LinearLayoutManager(this);
        mLinearManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(mLinearManager);
        RxUtils.bindEvents(mBtnRecharge,this);
        RxUtils.bindEvents(mBtnUnfreeze,this);
        registerPush();
    }

    private void registerPush() {
        Disposable disposable = RxBus.get().toFlowable(EventBean.class).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean bean) throws Exception {
                if (bean != null) {
                    if (bean.type == Constants.EVENT_TYPE_REFRESH) {
                        initInfo();
                    }
                }
            }
        });
        mCompositeDisposable.add(disposable);
    }

    /**
     * 初始化数据
     */
    private void initInfo() {
        getChange();
        getChangeDetail();
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_wallet_change_recharge:
                // 20180503 Liam :
                // 暂时先禁用充值功能。
                // JumpUtil.jumpToWalletActivity(this, WalletChangeRechargeInputActivity.class, bean);
                TipUtils.showToastLong(this, "暂停服务");
                break;
            case R.id.btn_wallet_change_unfreeze:
                JumpUtil.jumpToWalletActivity(this, WalletChangeUnfreezeActivity.class, bean);
                break;
        }
    }

    @Override
    protected void onRefreshData() {
        if (mAdapter.isLoading())
            mAdapter.loadMoreEnd();
        pageNum = 1;
        getChange();
        getChangeDetail();
    }

    /**
     * 获取零钱明细
     */
    private void getChangeDetail() {
        mPresenter.getTradeRecord(pageNum);
    }

    /**
     * 获取零钱明细回调
     *
     * @param list 查询到的数据
     */
    @Override
    public void getTradeRecordBack(List<TradeRecordBean> list) {
        if (mAdapter == null) {
            mAdapter = new WalletChangeAdapter(this, list);
            mRv.setAdapter(mAdapter);
            mAdapter.setOnLoadMoreListener(this, mRv);

        } else {
            if (mAdapter.isLoading())
                mAdapter.loadMoreComplete();
            if (pageNum == 1)
                mAdapter.replaceData(list);
            else mAdapter.addData(list);
        }
        mAdapter.setEnableLoadMore(list.size() < 20 ? false : true);
        mLlNone.setVisibility(list.isEmpty() && pageNum == 1 ? View.VISIBLE : View.GONE);
        refreshFinish();
    }

    @Override
    public void getChangeBack(UserAsset bean) {
        if (bean != null) {
            mTvChangeNum.setText(
                    TextUtils.isEmpty(bean.getCash_balance()) ? getString(R.string.wallet_zero_decimal) : bean.getCash_balance());
            mTvFrozen.setText(
                    TextUtils.isEmpty(bean.getCash_froze()) ? getString(R.string.wallet_zero_decimal) : bean.getCash_froze());
        } else {
            mTvChangeNum.setText(
                    getString(R.string.wallet_zero_decimal));
            mTvFrozen.setText(
                    getString(R.string.wallet_zero_decimal));
        }
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void onLoadMoreRequested() {
        pageNum++;
        getChange();
        getChangeDetail();
    }

    private void getChange() {
        mPresenter.getChange();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter != null && mAdapter.isLoading())
            mAdapter.loadMoreEnd();
        refreshFinish();

    }

}
